from numpy.random import binomial, shuffle, seed
import networkx as nx
seed(1)
n0 = 2048
p = 32
n1 = 256
T = 11
alpha = 0.01
beta = 0.004

N = [0] + [n0 + n1 * i for i in range(T)]
G = {i: set() for i in range(N[-1])}
labels = {i: i // (N[-1] // p) for i in range(N[-1])}
m = 0
H = nx.Graph()
for i in range(1, N[-1]):
    for j in range(i):
        w = binomial(1, alpha if labels[i] == labels[j] else beta)
        if w:
            G[i].add(j)
            G[j].add(i)
            H.add_edge(i, j)
        
order = list(range(N[-1]))
shuffle(order)
for t in range(T):
    print(t, H.subgraph(order[:N[t+1]]).number_of_edges())
    assert nx.is_connected(H.subgraph(order[:N[t+1]]))


curr = set()
for t in range(T):
    curr.update(order[N[t]:N[t+1]])
    with open('graph' + str(t) + '.txt', '+w') as graph:
        for i in order[N[t]:N[t+1]]:
            line = [i, labels[i]]
            for j in G[i]:
                if j in curr:
                    line.append(j)
            graph.write(' '.join(map(str, line)) + "\n")
        
with open('graph_total.txt', '+w') as total:
    for i in order:
        line = [i, labels[i]]
        for j in G[i]:
            line.append(j)
        total.write(' '.join(map(str, line)) + "\n")
    

#nx.draw(H, node_size=10)